#include "client.h"


int login_user(ClientInfo* client) {
    char username[MAX_USERNAME_LEN]={0};
    char password[MAX_PASSWORD_LEN]={0};
    Message msg={0};

    printf("请输入用户名: ");
    scanf("%s", username);
    printf("请输入密码: ");
    scanf("%s", password);

    // 构造登录消息
    msg.type = MSG_TYPE_LOGIN;
    msg.length = strlen(username) + strlen(password) + 2; // +2 for null terminators
    snprintf(msg.data, sizeof(msg.data), "%s,%s", username, password);

    // 发送登录消息
    if (send_message(client->socket, &msg) < 0) {
        perror("发送登录消息失败");
        return -1;
    }

    // 接收服务器响应
    if (receive_message(client->socket, &msg) < 0) {
        perror("接收服务器响应失败");
        return -1;
    }

    // 处理服务器响应
    if (msg.type == MSG_TYPE_LOGIN_SUCCESS) {
        strncpy(client->username, username, MAX_USERNAME_LEN);
        client->is_logged_in = true;
        strcpy(client->current_dir, "/");
        return 0; // 登录成功
    } else {
        return -1; // 登录失败
    }
}